package com.yxzx.config;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class CorsConfig {

    @Value("${spring.web.allowedOrigins}")
    private String allowedOrigins;

    @Bean
    public CorsFilter corsFilter(){


        UrlBasedCorsConfigurationSource source=new UrlBasedCorsConfigurationSource();


        CorsConfiguration config=new CorsConfiguration();
        config.setAllowCredentials(true);

        //循环获取配置信息，配置域
        String[] split = allowedOrigins.split(",");
        for(String s:split){
            config.addAllowedOrigin(s);
        }

        //指定接口路径可以跨域
        source.registerCorsConfiguration("/**",config);


        return new CorsFilter(source);
    }

}
